package jinzhi1.binary.binary.ui;

import jinzhi1.binary.binary.lib.Bin;
import jinzhi1.binary.binary.lib.Dec;
import jinzhi1.binary.binary.lib.Hex;
import jinzhi1.binary.binary.lib.Oct;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultCaret;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.SimpleDateFormat;
import java.util.BitSet;
import java.util.Date;

import static jinzhi1.binary.binary.ui.Setting.*;

/**
 * @Author: MuMu
 * @Date: 2024/4/7 19:25
 */
public class MainWindow extends JFrame {
    private final JPanel topPanel = new JPanel();
    private final JPanel versionPanel = new JPanel();
    private final JPanel inputPanel = new JPanel();

    private JTextField binTextField, octTextField, decTextField, hexTextField;
    private Bin bin;
    private Oct oct;
    private Dec dec;
    private Hex hex;


    public MainWindow(){
        initWindow();
        setLayout();
        key();
        zhuan();
        setVisible(true);
    }

    private void initWindow() {
        setTitle(Window_title);
        setSize(Window_width, Window_height);
        setLocationRelativeTo(null);
        setResizable(Window_resizable);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        ImageIcon icon = new ImageIcon("window_icon.png"); // 请替换为您的图标文件路径
        setIconImage(icon.getImage());

        addWindowListener(new MainWindowListener(this));
    }

    private void setLayout(){
        setLayout(new BorderLayout(30, 0));
        add(topPanel, BorderLayout.NORTH);
        add(inputPanel, BorderLayout.CENTER);
        add(versionPanel, BorderLayout.SOUTH);

        topLayout();
        inputLayout();
        versionLayout();
    }



    private void inputLayout(){
        inputPanel.setLayout(new GridLayout(4,1, 0, 50));
        inputPanel.setBorder(new EmptyBorder(50, 20, 0, 20));
        inputPanel.setBackground(Main_color);
        binTextField = addTextField(inputPanel, "二进制");
        octTextField = addTextField(inputPanel, "八进制");
        decTextField = addTextField(inputPanel, "十进制");
        hexTextField = addTextField(inputPanel, "16进制");

        binTextField.addFocusListener(new HighlightFocusListener());
        octTextField.addFocusListener(new HighlightFocusListener());
        decTextField.addFocusListener(new HighlightFocusListener());
        hexTextField.addFocusListener(new HighlightFocusListener());

    }

    private void topLayout(){
        //topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0,0));
        topPanel.setLayout(new BorderLayout(12,0));
        topPanel.setBackground(Main_color);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Font font = new Font("微软雅黑", Font.BOLD, 44);
        JLabel label = new JLabel(format.format(new Date()));
        label.setFont(font);
        label.setForeground(new Color(31, 105, 243));
        label.setBorder(new EmptyBorder(40, 20, 40, 20));

        JPanel textFieldPanel = new JPanel(new BorderLayout(12,0));
        textFieldPanel.setBorder(new EmptyBorder(2, 0, 0, 0));
        textFieldPanel.setBackground(Main_color2);

        topPanel.add(label, BorderLayout.CENTER);

        topPanel.add(textFieldPanel, BorderLayout.SOUTH);
        new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(format.format(new Date()));
            }
        }).start();


    }

    private void versionLayout(){
        versionPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0,0));
        versionPanel.setBorder(new EmptyBorder(40, 20, 30, 20));
        versionPanel.setBackground(Main_color);
        JLabel versionLabel = new JLabel("@MuMu    版本号: 1.0");
        versionLabel.setForeground(new Color(218, 218, 218));
        versionPanel.add(versionLabel, BorderLayout.CENTER);
    }



    private JTextField addTextField(JPanel panel, String labelText) {
        // 创建一个面板来包含标题和输入框
        JPanel textFieldPanel = new JPanel(new BorderLayout(12,0));
        textFieldPanel.setBackground(Main_color);

        // 创建标题标签并设置颜色
        JLabel label = new JLabel(labelText);
        Font font = new Font("微软雅黑", Font.BOLD, 24);
        label.setFont(font);
        label.setForeground(Input_lable_color);

        // 创建输入框
        JTextField textField = new JTextField();
        textField.setBorder(BorderFactory.createLineBorder(Input_border_color, 1,true));
        textField.setBackground(Main_color);
        Font font2 = new Font("微软雅黑", Font.PLAIN, 18);
        textField.setFont(font2);
        textField.setForeground(new Color(241, 241, 241));

        UIManager.put("TextField.caretForeground", Color.WHITE);
        // 将标题和输入框添加到textFieldPanel的顶部
        textFieldPanel.add(label, BorderLayout.WEST);
        textFieldPanel.add(textField, BorderLayout.CENTER);


        // 将textFieldPanel添加到主面板
        panel.add(textFieldPanel);

        return textField;
    }


   private void key(){
       //实时输入监听
       // 限制二进制输入框只能输入0和1
       binTextField.addKeyListener(new java.awt.event.KeyAdapter() {
           public void keyTyped(java.awt.event.KeyEvent evt) {
               char c = evt.getKeyChar();
               if (!(c == '0' || c == '1')) {
                   evt.consume(); // 忽略不合法的输入
               }
           }
       });

       // 限制八进制输入框只能输入0到7
       octTextField.addKeyListener(new java.awt.event.KeyAdapter() {
           public void keyTyped(java.awt.event.KeyEvent evt) {
               char c = evt.getKeyChar();
               if (c < '0' || c > '7') {
                   evt.consume(); // 忽略不合法的输入
               }
           }
       });

       // 限制八进制输入框只能输入0到7
       decTextField.addKeyListener(new java.awt.event.KeyAdapter() {
           public void keyTyped(java.awt.event.KeyEvent evt) {
               char c = evt.getKeyChar();
               if (c < '0' || c > '9') {
                   evt.consume(); // 忽略不合法的输入
               }
           }
       });



       // 限制十六进制输入框只能输入0到9和a到f（大小写均可）
       hexTextField.addKeyListener(new java.awt.event.KeyAdapter() {
           public void keyTyped(java.awt.event.KeyEvent evt) {
               char c = evt.getKeyChar();
               if (!(Character.isDigit(c) /*|| (c >= 'a' && c <= 'f')*/ || (c >= 'A' && c <= 'F'))) {
                   evt.consume(); // 忽略不合法的输入
               }
           }
       });
   }

   private void zhuan(){
       // 回车监听
       binTextField.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               binToOther();
           }
       });

       octTextField.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               octToOther();
           }
       });

       decTextField.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               decToOther();
           }
       });

       hexTextField.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               hexToOther();
           }
       });
   }


    private void binToOther(){
        String binaryText = binTextField.getText();
        if (!binaryText.isEmpty()) {
            bin = new Bin(binaryText);
            octTextField.setText(String.valueOf(bin.toOct()));
            decTextField.setText(String.valueOf(bin.toDec()));
            hexTextField.setText(bin.toHex());
        }
    }

    private void octToOther(){
        String octalText = octTextField.getText();
        if (!octalText.isEmpty()) {
            oct = new Oct(Long.parseLong(octalText));
            binTextField.setText(String.valueOf(oct.toBin()));
            decTextField.setText(String.valueOf(oct.toDec()));
            hexTextField.setText(oct.toHex());
        }
    }

    private void decToOther(){
        String decimalText = decTextField.getText();
        if (!decimalText.isEmpty()) {
            dec = new Dec(Long.parseLong(decimalText));
            binTextField.setText(String.valueOf(dec.toBin()));
            octTextField.setText(String.valueOf(dec.toOct()));
            hexTextField.setText(dec.toHex());
        }
    }


    private void hexToOther(){
        String hexadecimalText = hexTextField.getText();
        if (!hexadecimalText.isEmpty()) {
            hex = new Hex(hexadecimalText);
            binTextField.setText(hex.toBin());
            octTextField.setText(String.valueOf(hex.toOct()));
            decTextField.setText(String.valueOf(hex.toDec()));
        }
    }

}
